package com.zhuang.collection.exercise;

import java.util.ArrayList;

public class ListExercise02 {
	public static void main(String[] args) {
		// List list = new ArrayList();
		ArrayList<Book> list = new ArrayList<>();

		// List list = new Vector();
		list.add(new Book("红楼梦", "曹雪芹", 100));
		list.add(new Book("西游记", "吴承恩", 10));
		list.add(new Book("水浒传", "施耐庵", 19));
		list.add(new Book("三国", "罗贯中", 80));

		// 遍历
		System.out.println("====排序前====");
		for (Book o : list) {
			System.out.println(o);
		}
		System.out.println("====排序后====");
		sort(list);
		for (Book o : list) {
			System.out.println(o);
		}
	}

	// 静态方法
	// 价格要求是从小到大 冒泡排序
	public static void sort(ArrayList<Book> list) {
		int listSize = list.size();
		for (int i = 0; i < listSize - 1; i++) {
			for (int j = 0; j < listSize - 1 - i; j++) {
				// 取出对象 Book
				Book book1 = (Book) list.get(j);
				Book book2 = (Book) list.get(j + 1);
				if (book1.getPrice() > book2.getPrice()) {// 交换
					list.set(j, book2);
					list.set(j + 1, book1);
				}
			}
		}
	}

	static class Book {
		private String name;
		private String author;
		private double price;

		public Book(String name, String author, double price) {
			this.name = name;
			this.author = author;
			this.price = price;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getAuthor() {
			return author;
		}

		public void setAuthor(String author) {
			this.author = author;
		}

		public double getPrice() {
			return price;
		}

		public void setPrice(double price) {
			this.price = price;
		}

		@Override
		public String toString() {
			return "Book{" + "name='" + name + '\'' + ", author='" + author + '\'' + ", price=" + price + '}';
		}
	}
}
